﻿
using System.Net.Http;
using System.Net.Http.Headers;

namespace BookManege.service
{
    internal class MyHttpRespons
    {
        public static string key = "52634041db48dccb50fa03b58b4e76cf";
        private readonly HttpClient _httpClient;
        public MyHttpRespons()
        {
            _httpClient = new HttpClient();
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
        public async Task<string> GetResponseBook(string ISBN)
        {
            try
            {
                HttpResponseMessage response = await _httpClient.GetAsync("http://api.tanshuapi.com/api/isbn/v1/index?key="+ key+ "&isbn="+ISBN);
                response.EnsureSuccessStatusCode(); // 确保请求成功  
                return await response.Content.ReadAsStringAsync(); // 读取响应内容  
            }
            catch (HttpRequestException e)
            {
                MessageBox.Show("请求失败"+e.Message);
                return null;
            }
        }
        public async Task<Byte[]> LoadImageFromUrlAsync(string url)
        {
            try
            {
                return  await _httpClient.GetByteArrayAsync(url);
            }
            catch (Exception ex)
            {
                MessageBox.Show("图片加载失败" + ex.Message);
                return null;
            }
        }
    }
}
